package com.collect.senegal.zhiScene.zhiWidget.zhiFilterAdminGroup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.senegal.MyApplication;
import com.collect.senegal.R;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDay;
import com.collect.senegal.zhiUtils.StringUtil;
import com.collect.senegal.zhiUtils.ToastUtil;

import org.jaaksi.pickerview.dialog.DefaultPickerDialog;
import org.jaaksi.pickerview.picker.OptionPicker;
import org.jaaksi.pickerview.widget.DefaultCenterDecoration;
import org.jaaksi.pickerview.widget.PickerView;

import java.util.ArrayList;

public class ZHIRangePicker extends RelativeLayout {

    private final Context mContext;

    private final String mMainText;

    private TextView tv_content;

    private ImageView iv_content_del;

    private OptionPicker mPicker;

    private ArrayList<ZHIRangeDay> mRangeDayList;

    public boolean mOnlyShowDefaultValue = false;

    public ZHIRangePicker(Context context) {
        this(context, null);
    }

    public ZHIRangePicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIRangePicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZHIRangePicker);
        mMainText = ta.getString(R.styleable.ZHIRangePicker_mainText);
        ta.recycle();

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        View mView = View.inflate(mContext, R.layout.view_range_picker, this);

        TextView tv_title = mView.findViewById(R.id.tv_title);
        tv_title.setText(mMainText);

        tv_content = mView.findViewById(R.id.tv_content);
        iv_content_del = mView.findViewById(R.id.iv_content_del);

        LinearLayout ll_compare = mView.findViewById(R.id.ll_compare);
        ll_compare.setOnClickListener(v -> {
            if (mOnlyShowDefaultValue) {
                return;
            }

            showPickerDialog();
        });

        iv_content_del.setOnClickListener(v -> {
            if (mOnlyShowDefaultValue) {
                return;
            }

            String startDate = StringUtil.safeString(tv_content.getText().toString().trim());
            if (startDate.equalsIgnoreCase(mContext.getString(R.string.compare))) {
                showPickerDialog();
            } else {
                updateCompare(true, null);
            }
        });
    }

    private void initOptionPicker() {
        try {
            OptionPicker.OnOptionSelectListener mPickerSelectListener = (picker, selectedPosition, selectedOptions) -> {
                try {
                    ZHIRangeDay zhiRangeDay = (ZHIRangeDay) selectedOptions[0];
                    updateCompare(false, zhiRangeDay.getDayTitle());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            mPicker = new OptionPicker.Builder(mContext, 1, mPickerSelectListener)
                    .setInterceptor((pickerView, params) -> pickerView.setVisibleItemCount(5)).create();

            PickerView.sOutTextSize = 16;
            PickerView.sCenterTextSize = 18;
            PickerView.sCenterColor = mContext.getResources().getColor(R.color.theme_color_0);
            PickerView.sOutColor = Color.GRAY;
            PickerView.sDefaultVisibleItemCount = 5;

            DefaultCenterDecoration.sDefaultLineWidth = 1;
            DefaultCenterDecoration.sDefaultLineColor = mContext.getResources().getColor(R.color.theme_color_0);

            DefaultPickerDialog dialog = (DefaultPickerDialog) mPicker.dialog();
            dialog.getTitleView().setText(mContext.getString(R.string.pls_select));

            TextView btnCancel = (TextView) dialog.getBtnCancel();
            btnCancel.setText(mContext.getString(R.string.cancel));
            btnCancel.setVisibility(GONE);

            TextView btnConfirm = (TextView) dialog.getBtnConfirm();
            btnConfirm.setText(mContext.getString(R.string.confirm));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateCompare(boolean reset, String data) {
        try {
            if (reset) {
                tv_content.setTextColor(mContext.getResources().getColor(R.color.theme_transparent_7));
                tv_content.setText(mContext.getString(R.string.compare));
                iv_content_del.setImageResource(R.drawable.ic_125);
                return;
            }

            if (TextUtils.isEmpty(data)) {
                return;
            }

            tv_content.setText(data);
            tv_content.setTextColor(mContext.getResources().getColor(R.color.theme_color_0));
            iv_content_del.setImageResource(R.drawable.ic_124);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showPickerDialog() {
        try {
            if (mPicker == null) {
                initOptionPicker();
            }

            if (mRangeDayList != null && mRangeDayList.size() > 0) {
                mPicker.setData(mRangeDayList);
                mPicker.show();
            } else {
                ToastUtil.s(MyApplication.getAppContext().getString(R.string.list_data_is_empty));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getCompare() {
        String tempValue = StringUtil.safeString(tv_content.getText().toString().trim());
        if (tempValue.equalsIgnoreCase(mContext.getResources().getString(R.string.compare))) {
            tempValue = "";
        }
        return tempValue;
    }

    public void reset() {
        if (mOnlyShowDefaultValue) {
            return;
        }
        updateCompare(true, null);
    }

    public void setDataSource(ArrayList<ZHIRangeDay> dataSource) {
        try {
            if (mRangeDayList == null) {
                mRangeDayList = new ArrayList<>();
            }
            mRangeDayList.clear();
            mRangeDayList.addAll(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
